<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Plus" type="primary" @click="handleEdit($event)">
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form inline :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              clearable
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" border :data="list">
      <el-table-column align="center" label="序号" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="id"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="用户名"
        prop="name"
        show-overflow-tooltip
      />

      <el-table-column align="center" label="账号" show-overflow-tooltip>
        <template #default="{ row }">
          <!-- <el-tag v-for="(item, index) in row.account" :key="index">
                {{ item }}
              </el-tag> -->
          {{ row.account }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="角色">
        <template #default="{ row }">
          <el-tag v-for="(item, index) in row.role" :key="index">
            {{ item.name }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="修改时间"
        prop="update_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="200"
      >
        <template #default="{ row }">
          <div class="flex-btn">
            <el-button type="primary" text @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button
              v-if="row['is_super'] !== 1"
              type="danger"
              text
              @click="handleDelete(row)"
            >
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.limit"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, getList } from '@/api/adminUser'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'PlatformUserList',
    components: {
      Edit: defineAsyncComponent(() => import('./components/adminUserEdit')),
    },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        list: [],
        memberlist: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          page: 1,
          limit: 10,
          name: '',
        },
      })

      const handleEdit = (row) => {
        console.log("state['editRef']", state['editRef'])
        if (row.id) {
          state['editRef'].showEdit(row)
        } else {
          state['editRef'].showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ id: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.limit = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.page = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.page = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { data, total },
        } = await getList(state.queryForm)
        state.list = data.map((item) => {
          item.department_id =
            item.department_id === 0 ? null : item.department_id
          return item
        })
        state.total = total
        state.listLoading = false
      }

      onMounted(async () => {
        await fetchData()
      })

      return {
        ...toRefs(state),
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>

<style scoped lang="scss">
  .flex-btn {
    display: flex;
    width: 100%;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
  }
</style>
